﻿@page "/"
@using BlazorApp1.Data
@using Microsoft.EntityFrameworkCore
@attribute [StreamRendering]
@rendermode InteractiveServer

@inject ApplicationDbContext Context;
@inject LoanService LoanService;
@inject UserService UserService;

<AuthorizeView>
    <Authorized>
        <PageTitle>租借管理</PageTitle>
        
        <div class="row">
            <div class="col-md-4">
                <label>设备名称: </label>
                <input class="form-control" readonly="readonly" @bind="_device.Name"/>
            </div>
            <div class="col-md-4">
                <label>租借人员: </label>
                <div class="input-group">
                    <input class="form-control" readonly="readonly" @bind="_user.Name"/>
                    <button class="btn btn-primary" @onclick="ReadCard">读卡</button>
                </div>
            </div>
            <div class="col-md-4">
                <label>备注: </label>
                <input class="form-control" @bind="_remark"/>
            </div>
        </div>
        <div class="float-right">
            <button class="btn btn-primary" disabled="@(!AllFieldsFilled())" @onclick="Approval">
                @if (_device.State)
                {
                    <text>归还审批</text>
                }
                else
                {
                    <text>租借审批</text>
                }
            </button>
        </div>
    </Authorized>
    <NotAuthorized>
        欢迎，请登录！
    </NotAuthorized>
</AuthorizeView>

@code {
    private DeviceInfo _device = new DeviceInfo();
    private UserInfo _user = new UserInfo();
    private string _remark;

    private List<DeviceInfo> _deviceInfos = new();
    private List<UserInfo> _userInfos = new();

    protected override async Task OnInitializedAsync()
    {
        _deviceInfos = await Context.DeviceInfo.ToListAsync();
        _userInfos = await Context.UserInfo.ToListAsync();

        LoanService.Action = null;
        LoanService.Action += Action;
    }

    private async void Action(string id)
    {
        var r = _deviceInfos.FirstOrDefault(o=>o.Id == id);

        if (r == null) return;
        
        await InvokeAsync(() =>
        {
            _device = r;
            StateHasChanged();
        });
    }

    bool AllFieldsFilled()
    {
        return !string.IsNullOrWhiteSpace(_device.Name) && 
               !string.IsNullOrWhiteSpace(_user.Name);
    }

    private async void Approval()
    {
        if (_device.State)
        {
            var info = await Context.LoanInfo.FirstOrDefaultAsync(o => o.DeviceId == _device.Id && o.ReturnTime == DateTime.MinValue);
            if (info != null)
            {
                info.ReturnTime = DateTime.Now;
                Context.LoanInfo.Update(info);
                _device.State = false;
            }
        }
        else
        {
            var info = new LoanInfo()
            {
                Id = Guid.NewGuid().ToString(),
                DeviceId = _device.Id,
                Leaseholder = _user.Id,
                LoanTime = DateTime.Now,
                Remark = _remark
            };

            await Context.LoanInfo.AddAsync(info);

            _device.State = true;
        }

        Context.DeviceInfo.Update(_device);
        await Context.SaveChangesAsync();

        _device = new DeviceInfo();
        _user = new UserInfo();
    }

    private void ReadCard()
    {
        var id = UserService.ReadCard();
        _user = Context.UserInfo.FirstOrDefault(o => o.Id == id);
    }
}